#include "Phoenix.h"

#include "../qcommon/qcommon.h"
#include <float.h>

HINSTANCE	global_hInstance;
qboolean	Minimized;
unsigned	sys_msg_time;

void Phoenix::setup()
{
	try
	{
		global_hInstance = GetModuleHandle( NULL );
		Qcommon_Init( 0, NULL );
	}
	catch ( ... )
	{
		MessageBox( NULL, L"There was a problem during startup.", L"Error", MB_ICONERROR | MB_OK );
		throw;
	}
}

int Phoenix::run()
{
	MSG				msg;
	int				time, oldtime, newtime;

	oldtime = Sys_Milliseconds ();

	for (;;)
	{
		// if at a full screen console, don't update unless needed
		if (Minimized || (dedicated && dedicated->value) )
		{
			Sleep (1);
		}

		while (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
		{
			if (!GetMessage (&msg, NULL, 0, 0))
				Com_Quit ();
			sys_msg_time = msg.time;
			TranslateMessage (&msg);
			DispatchMessage (&msg);
		}

		do
		{
			newtime = Sys_Milliseconds ();
			time = newtime - oldtime;
		} while (time < 1);
//			Con_Printf ("time:%5.2f - %5.2f = %5.2f\n", newtime, oldtime, time);

		//	_controlfp( ~( _EM_ZERODIVIDE /*| _EM_INVALID*/ ), _MCW_EM );
		_controlfp( _PC_24, _MCW_PC );
		Qcommon_Frame (time);

		oldtime = newtime;
	}

	return TRUE;
}

RegisterApplication( Phoenix );
